package com.bbm.communitysystem.service.impl;


import com.bbm.communitysystem.domain.Comment;
import com.bbm.communitysystem.domain.LikePost;
import com.bbm.communitysystem.domain.Post;
import com.bbm.communitysystem.domain.bo.LikePostBo;
import com.bbm.communitysystem.mapper.LikePostMapper;
import com.bbm.communitysystem.service.ILikePostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author transitory
 * @since 2025-02-20
 */
@Service
@RequiredArgsConstructor
public class LikePostServiceImpl extends ServiceImpl<LikePostMapper, LikePost> implements ILikePostService {

    private final PostServiceImpl postService;

    private final CommentServiceImpl commentService;

    /**
     * 点赞 帖子
     * @param likePost
     * @return
     */
    @Override
    @Transactional
    public int likePost(LikePost likePost) {

        LikePost one = lambdaQuery()
                .eq(LikePost::getSourceId, likePost.getSourceId())
                .eq(LikePost::getFounder, likePost.getFounder())
                .eq(LikePost::getType, likePost.getType())
                .one();

        if (one != null && one.getStatus() != 1) {
            one.setStatus(1);
            boolean b = updateById(one);

            if (!b) {
                throw new RuntimeException("点赞失败");
            }

            boolean update = postService.lambdaUpdate()
                    .setSql("likes = likes + 1")
                    .eq(Post::getId, likePost.getSourceId())
                    .update();

            if (!update) {
                throw new RuntimeException("点赞失败");
            }

        }

        if (one == null) {
            likePost.setCreateTime(String.valueOf(System.currentTimeMillis()));
            boolean save = save(likePost);
            if (!save) {
                throw new RuntimeException("点赞失败");
            }

            boolean update = postService.lambdaUpdate()
                    .setSql("likes = likes + 1")
                    .eq(Post::getId, likePost.getSourceId())
                    .update();

            if (!update) {
                throw new RuntimeException("点赞失败");
            }

        }
        return 0;
    }

    /****
     * 删除点赞帖子
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delLikePost(Integer id) {

        if (getById(id).getStatus() == 0) {
            return 1;
        }

        boolean update = lambdaUpdate()
                .eq(LikePost::getId, id)
                .set(LikePost::getStatus, 0)
                .eq(LikePost::getStatus, 1)
                .update();

        if (!update) {
            throw new RuntimeException("删除失败");
        }
        boolean update1 = postService.lambdaUpdate()
                .setSql("likes = likes - 1")
                .eq(Post::getId, getById(id).getSourceId())
                .update();

        if (!update1) {
            throw new RuntimeException("删除失败");
        }

        return 0;
    }

    /****
     * 删除点赞帖子2
     */
    @Override
    @Transactional
    public int delLikePost(LikePostBo likePostBo) {

        boolean update = lambdaUpdate()
                .eq(LikePost::getSourceId, likePostBo.getSourceId())
                .eq(LikePost::getFounder, likePostBo.getFounder())
                .eq(LikePost::getType, 0)
                .set(LikePost::getStatus, 0)
                .update();

        if (!update) {
            throw new RuntimeException("删除失败");
        }

        LikePost one = lambdaQuery()
                .eq(LikePost::getSourceId, likePostBo.getSourceId())
                .eq(LikePost::getFounder, likePostBo.getFounder())
                .eq(LikePost::getType, 0)
                .one();

        boolean update1 = postService.lambdaUpdate()
                .eq(Post::getId, one.getSourceId())
                .setSql("likes = likes - 1")
                .update();

        if (!update1) {
            throw new RuntimeException("删除失败");
        }


        return 0;
    }

    /**
     * 点赞评论
     * @param likePostBo
     * @return
     */
    @Override
    @Transactional
    public int likeComment(LikePostBo likePostBo) {

        LikePost likePost = new LikePost();

        // 点赞PO对象组装
        likePost.setType(likePostBo.getType());
        likePost.setStatus(likePostBo.getStatus());
        likePost.setSourceId(likePostBo.getCommentId());
        likePost.setFounder(likePostBo.getFounder());
        likePost.setCreateTime(System.currentTimeMillis() + "");


        LikePost one = lambdaQuery()
                .eq(LikePost::getSourceId, likePostBo.getCommentId())
                .eq(LikePost::getFounder, likePostBo.getFounder())
                .eq(LikePost::getType, likePostBo.getType())
                .one();

        if (one != null && one.getStatus() != 1) {
            one.setStatus(1);
             boolean update1 = lambdaUpdate()
                    .eq(LikePost::getId, one.getId())
                    .eq(LikePost::getType, 1)
                    .set(LikePost::getStatus, 1)
                    .set(LikePost::getCreateTime, System.currentTimeMillis() + "")
                    .update();

            if (!update1) {
                throw new RuntimeException("点赞失败");
            }

            //评论点赞加一
            boolean update = commentService.lambdaUpdate()
                    .setSql("comment_like = comment_like + 1")
                    .eq(Comment::getId, likePostBo.getCommentId())
                    .update();

            if (!update) {
                throw new RuntimeException("点赞失败");
            }

        }


        if (one == null) {
            boolean save = save(likePost);

            if (!save) {
                throw new RuntimeException("点赞失败");
            }

            //评论点赞加一
            boolean update = commentService.lambdaUpdate()
                    .setSql("comment_like = comment_like + 1")
                    .eq(Comment::getId, likePostBo.getCommentId())
                    .update();

            if (!update) {
                throw new RuntimeException("点赞失败");
            }
        }

        return 0;
    }

    /**
     * 删除评论点赞
     * @param likePostBo
     * @return
     * */
    @Override
    @Transactional
    public int delLikeComment(LikePostBo likePostBo) {

        boolean update = lambdaUpdate()
                .eq(LikePost::getSourceId, likePostBo.getCommentId())
                .set(LikePost::getStatus, 0)
                .update();

        if (!update) {
            throw new RuntimeException("删除失败");
        }

        boolean update1 = commentService.lambdaUpdate()
                .setSql("comment_like = comment_like - 1")
                .eq(Comment::getId, likePostBo.getCommentId())
                .update();

        if (!update1) {
            throw new RuntimeException("删除失败");
        }

        return 0;
    }


}
